/* MoonView.m
 * Part of the Moon application for the NeXT computer.
 * Author:  Geoffrey S. Knauth
 * Date:    January 4, 1992
 *
 * Permission to copy this program is hereby granted under the terms
 * of the Free Software Foundation's GNU General Public License.
 */

#import <appkit/Application.h>		/* NXApp */
#import <appkit/NXImage.h>
#import <appkit/Window.h>
#import "MoonView.h"
#import "HugeMoonView.h"
#import "all.h"

/* These measures of the moon's edges were taken manually via:
 *   - mouseDown:(NXEvent *)e
 *   {   NXPoint local = e->location;
 *       [self convertPoint:&local fromView:nil];
 *       printf("mouseDown: view coordinates %.f, %.f\n", local.x, local.y);  
 *       return self;
 *   }
 */
struct moonInfo fullMoon = {208., 16., 50., 238.};	/* FullMoon.tiff */

@implementation MoonView

- initFrame:(const NXRect *)frameRect
{
  /* 288, 228 --> This is the size that IB should know about. */
    NXSize rectSize = {1152/4, 912/4};

    [super initFrame:frameRect];

  /* This is for the 1/4 size MoonView in the main application window. */
    image = [[NXImage alloc] initFromSection:"FullMoon.tiff"];
    [[image setSize:&rectSize] setScalable:YES];

  /* This is for the moon itself, that is, not the stars and space
   * that make up the background.
   */
    xradius = (fullMoon.right - fullMoon.left) / 2.;
    yradius = (fullMoon.top - fullMoon.bottom) / 2.;
    center.x = fullMoon.left + xradius;
    center.y = fullMoon.bottom + yradius;
    [self setPhase:0.0];

    return self;
}

- drawSelf :(const NXRect *)rects :(int)rectCount
{
  /* draw the image of the moon */
    [image composite:NX_COPY toPoint:&bounds.origin];

  /* now draw the shadow over that part hidden from the sun */
    DrawMoonShadow([self phase], &center, xradius, yradius, NX_BLACK);

    return self;
}

- free
{
    [image free];		/* Free the 1/4 size image on our way out. */
    if (hugeWindow)		/* See if full size window and image exist. */
	[hugeWindow free];	/* This will also free the view and image. */
    return [super free];
}

- mouseDown:(NXEvent *)theEvent
{
    NXRect hugeRect = {{0., 0.}, {1152., 912.}};    /* full screen */

    if (theEvent->type == NX_MOUSEDOWN) {
	if (theEvent->data.mouse.click > 1) {
	    [NXApp getNextEvent:NX_MOUSEUPMASK];    /* wait for mouse up */
	    if (hugeWindow == nil) {
		hugeWindow = [[Window alloc] initContent:&hugeRect
			  style:NX_PLAINSTYLE backing:NX_RETAINED
			  buttonMask:0 defer:YES];
		hugeMoonView = [[HugeMoonView alloc] initFrame:&hugeRect];
		[hugeWindow setContentView:hugeMoonView];
	    }
	    [hugeWindow makeKeyAndOrderFront:self];
	    [hugeMoonView display];		    /* Cheese! */
	    [NXApp getNextEvent:NX_MOUSEDOWNMASK];  /* absorb mouse down */
	    [NXApp getNextEvent:NX_MOUSEUPMASK];    /* and mouse up */
	    [hugeWindow orderOut:self];		    /* put huge moon away */
	}
    }
    return self;
}

- (float)phase
{
    return phase;
}

- setPhase :(float)aFloat;
{
    phase = aFloat;
    return self;
}

@end
